package zjh_20230308;

import javax.lang.model.element.Element;
import java.util.ArrayList;
import java.util.Arrays;

public class arrayList extends ArrayList {
    private Object[] data;
    private int size;

    public arrayList() {
        data = new Object[1];
        size = 0;
    }

    public Object[] getData() {
        return data;
    }

    public void setData(Object[] data) {
        this.data = data;
    }

    public int getSize() {
        return size;
    }

    public void setSize(int size) {
        this.size = size;
    }

    /**
     * 添加元素到末尾 boolean add(E e)
     */
    public boolean add(Object element) {
        if (size == data.length) {
            grow();
        }
        data[size++] = element;
        System.out.println(Arrays.toString(data));
        return true;
    }

    /**
     * 添加元素到指定下标
     */
    public void add(int index, Object element) {
        grow();
        for (int i = data.length - 2; i >= index; i--) {
            data[i + 1] = data[i];
        }
        data[index] = element;
//        System.out.println(data[0]);
        System.out.println(Arrays.toString(data));
    }

    /**
     * 扩容
     */
    private void grow() {
        Object[] newData = new Object[data.length + 1];
        System.arraycopy(data, 0, newData, 0, size);
        data = newData;
    }

    /**
     * 清除列表中的元素
     */

    public void clear() {
        data = new Object[0];
        System.out.println(Arrays.toString(data));
    }

    /**
     * 判断是否为空
     */
    public boolean isEmpty() {
        if (size() == 0) {
            return true;
        }
        return false;
    }

    /**
     * 列表大小
     */
    public int size() {
        return data.length;
    }

    /**
     * 判断是否相等
     */
    public boolean equals(Object o) {
        if (o == this) {
            return true;
        }
        return false;
    }

    /**
     * 移除指定位置的元素,并返回此元素
     */
    public Object remove(int index) {
        Object a = data[index];
        for (int i = index; i < data.length - 1; i++) {
            data[index] = data[index + 1];
        }
        Object[] newData = new Object[data.length - 1];
        System.arraycopy(data, 0, newData, 0, newData.length);
        data = newData;
        return a;
    }

    /**
     * 返还一个数组
     */
    public Object[] toArray() {
        return Arrays.copyOf(data, size);
    }


    /**
     * 第一次出现元素的下标
     *
     * @return
     */
    public int indexOf(Object o) {
        return indexOfRange(o, 0, size);
    }

    int indexOfRange(Object o, int start, int end) {
        Object[] es = data;
        if (o == null) {
            for (int i = start; i < end; i++) {
                if (es[i] == null) {
                    return i;
                }
            }
        } else {
            for (int i = start; i < end; i++) {
                if (o.equals(es[i])) {
                    return i;
                }
            }
        }
        return -1;
    }

    /**
     * 判断是否包含
     * @return
     */
    public boolean contain(Object o){
        return indexOf(o) >= 0;
    }
    @Override
    public String toString() {
        return "arrayList{" +
                "data=" + Arrays.toString(data) +
                ", size=" + size +
                '}';
    }
}
